<template>
  <div id="search">
    <div class="title" @click="goback"><span>&lt;</span> 搜索</div>
    <div class="search">
      <input
        type="text"
        placeholder="搜索商家、商品名称"
        v-model="inputText"
      /><span @click="search">搜索</span>
    </div>
    <div class="box" v-show="searchstate == 0">
      <h4>历史搜索 <span @click="del">🚽</span></h4>
      <div class="list">
        <div class="find" v-for="(item, index) in findlist" :key="index">
          {{ item.searchkey }}
        </div>
      </div>
    </div>
    <div class="box" v-show="searchstate == 0">
      <h4>搜索发现</h4>
      <div class="list">
        <span class="find" v-for="(item, index) in histrylist" :key="index">{{
          item.name
        }}</span>
      </div>
    </div>
    <div class="searchlist" v-show="searchstate == 1">
      <div class="searchnav">
        <span
          :class="{ active: nowindex == index }"
          @click="option(index)"
          v-for="(item, index) in searchnav"
          :key="index"
          >{{ item.option }}</span
        >
      </div>
      <div @click="goshop(item.mtWmPoiId)" class="serchitem" v-for="(item,index) in searchlist" :key="index">
        <div class="shoppic">
          <img :src="item.picUrl" alt="" />
        </div>
        <div class="shopdetails">
          <div class="name">{{item.name}}</div>
          <div class="det">
            <span class="grade">☆{{item.wmPoiScore/10}}</span>
            <span class="many">{{item.monthSalesTip}}</span>
            <span class="fast">{{item.distance}}</span>
          </div>
          <div class="det2">本店支持开发票，开票金额100元起</div>
          <div class="goodspic">
            <img src="" alt="" />
            <div class="much"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { search } from "../api";
export default {
  data() {
    return {
      searchstate: 0,
      inputText: "",
      findlist: [],
      searchlist: [],
      nowindex: 0,
      searchnav: [
        {
          option: "综合排序∨",
        },
        {
          option: "距离最近",
        },
        {
          option: "销量最高",
        },
        {
          option: "筛选▽",
        },
      ],
      histrylist: [
        { name: "麻辣烫" },
        { name: "麻辣米线" },
        { name: "汉堡" },
        { name: "烧烤" },
        { name: "日料" },
        { name: "火锅" },
        { name: "韩餐" },
        { name: "砂锅" },
        { name: "酒店" },
      ],
    };
  },
  methods: {
    option(index) {
      this.nowindex = index;
      if (index == 1) {
        this.searchlist.sort((a, b) => {
          return parseInt(a.distance) - parseInt(b.distance);
        });
      } else if (index == 2) {
        this.searchlist.sort((a, b) => {
          return (
            parseInt(b.monthSalesTip.substr(2)) -
            parseInt(a.monthSalesTip.substr(2))
          );
        });
      } else if (index == 0) {
        var obj = { searchkey: this.inputText };
        search(obj).then((res)=>{
            this.searchlist = res.data.list;
        })
      }
    },
    search() {
      if (this.inputText) {
        this.searchstate = 1
        var obj = { searchkey: this.inputText };
        this.findlist.push(obj);
        search(obj).then((res)=>{
            // console.log(res.data);
            this.searchlist = res.data.list;
        })
      }
      this.inputText = "";
    },
    del() {
      this.findlist = [];
    },
    goback() {
      this.$router.push("/home/homepage");
    },
    goshop(id){
      console.log(id);
      this.$router.push({path:'/shop/'+id})
    }
  },
};
</script>

<style scoped>
#search .title {
  line-height: 40px;
  font-weight: 700;
}

#search .title span {
  color: lightskyblue;
}

#search {
  padding: 10px;
}

#search .search {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#search .search input {
  width: 250px;
  height: 35px;
  border-radius: 15px;
  border: 0;
  background-color: rgb(239, 237, 237);
  padding-left: 20px;
}

#search .search span {
  color: lightskyblue;
}

#search .box {
  margin-top: 20px;
}

#search .box h4 span {
  color: gray;
  float: right;
  font-size: 12px;
  font-weight: 400;
}

#search .box .list {
  font-size: 12px;
  margin-top: 5px;
  width: 100%;
}

#search .box .list .find {
  padding: 5px;
  background-color: rgb(239, 237, 237);
  display: inline-block;
  margin: 5px;
}
#search .searchlist .searchnav {
  font-size: 16px;
  font-weight: 700;
  line-height: 40px;
  display: flex;
  justify-content: space-around;
}
#search .searchlist .searchnav > span.active {
  color: rgb(43, 163, 237);
}
#search .searchlist .serchitem {
  display: flex;
}
#search .searchlist .serchitem .shoppic {
  width: 60px;
  margin-right: 10px;
}
#search .searchlist .serchitem .shoppic img {
  width: 100%;
}
#search .searchlist .serchitem .shopdetails {
    flex: 1;
}
#search .searchlist .serchitem .shopdetails .name{
    font-weight: 700;
    line-height: 40px;
}
#search .searchlist .serchitem .shopdetails .det{
    font-size: 12px;
}
#search .searchlist .serchitem .shopdetails .det .grade{
    color: orangered;
}
#search .searchlist .serchitem .shopdetails .det .many{
    color: #ccc;
}
#search .searchlist .serchitem .shopdetails .det .fast{
    color: #ccc;
    float: right;
}
#search .searchlist .serchitem .shopdetails .det2{
    font-size: 12px;
    line-height: 40px;
    background-color: rgb(248, 211, 142);
    display: inline;
    color: rgb(255, 136, 0);
}
#search .searchlist .serchitem .shopdetails .goodspic{
    width: 150px;
}
#search .searchlist .serchitem .shopdetails .goodspic img{
    width: 100%;
}
#search .searchlist .serchitem .shopdetails .goodspic .much{
    color: red;
}
</style>